package org.company.tool.service;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.company.tool.bean.Entity;

/**
 * 路径转化给则，根据名称查询获取相应规则的路径
 * @author hucw
 *
 */
public class PathTransfer {
    
    private Entity entity;
    
    //java文件根目录
    private String javaDir;
    
    //web文件根目录
    private String webDir;
    
    public enum Path {
        PO,SQLMAP,IDAO,DAOIMPL,ISERVICE,SERVICEIMPL,ACTION,JSP,JS,POOBJ,DAOOBJ,SERVICEOBJ
    }
    
    protected final class Pair{
        public String packageName;
        public String beanName;
    }
    
    protected String PO_NAME="${ClassName}PO";
    protected String ACTION_NAME="${ClassName}Action";
    protected String IDAO_NAME="I${ClassName}DAO";
    protected String ISERVICE_NAME="I${ClassName}Service";
    protected String DAO_IMPL_NAME="${ClassName}DAOImpl";
    protected String SERVICE_IMPL_NAME="${ClassName}ServiceImpl";
    protected String SQLMAP_NAME="${ClassName}SQL";
    protected String JSP_NAME="${entityName}";
    protected String JS_NAME="${entityName}";
    protected String PO_PACKAGE="${package}.persistece.po";
    protected String ACTION_PACKAGE="${package}.web.action.${entityName}";
    protected String IDAO_PACKAGE="${package}.persistece.po";
    protected String ISERVICE_PACKAGE="${package}.web.service";
    protected String DAO_IMPL_PACKAGE="${package}.persistece.po.impl";
    protected String SERVICE_IMPL_PACKAGE="${package}.web.service.impl";
    protected String SQLMAP_PACKAGE="${package}.persistece.sql";
    protected String JSP_PACKAGE="${entityName}";
    protected String JS_PACKAGE="js";
    protected String PO_OBJ_NAME="${entityName}PO";
    protected String DAO_OBJ_NAME="${entityName}DAO";
    protected String SERVICE_OBJ_NAME="${entityName}Service";
    
    public PathTransfer(Entity entity,String baseDir) {
        this.entity = entity;
        this.javaDir = baseDir + File.separator + "src" +
                        File.separator + "main" + 
                        File.separator + "java";
        this.webDir = baseDir + File.separator + "src" + 
                        File.separator + "main" + 
                        File.separator + "webapp";
    }
    
    private String replaceAll(String target) {
        String entityName = StringUtils.uncapitalize(entity.getJavaName());
        String className = entity.getJavaName();
        String packageName = entity.getPackageName();
        return target.replace("${package}", packageName).
                replace("${className}", className).
                replace("${entityName}", entityName);
    }
    
    private Pair transferPair(Path path) {
        Pair pair = new Pair();
        switch(path) {
        case PO:
            pair.packageName = replaceAll(PO_PACKAGE);
            pair.beanName = replaceAll(PO_NAME);
            break;
        case SQLMAP:
            pair.packageName = replaceAll(SQLMAP_PACKAGE);
            pair.beanName = replaceAll(SQLMAP_NAME);
            break;
        case IDAO:
            pair.packageName = replaceAll(IDAO_PACKAGE);
            pair.beanName = replaceAll(IDAO_NAME);
            break;
        case DAOIMPL:
            pair.packageName = replaceAll(DAO_IMPL_PACKAGE);
            pair.beanName = replaceAll(DAO_IMPL_NAME);
            break;
        case ISERVICE:
            pair.packageName = replaceAll(ISERVICE_PACKAGE);
            pair.beanName = replaceAll(ISERVICE_NAME);
            break;
        case SERVICEIMPL:
            pair.packageName = replaceAll(SERVICE_IMPL_PACKAGE);
            pair.beanName = replaceAll(SERVICE_IMPL_NAME);
            break;
        case ACTION:
            pair.packageName = replaceAll(ACTION_PACKAGE);
            pair.beanName = replaceAll(ACTION_NAME);
            break;
        case JSP:
            pair.packageName = replaceAll(JSP_PACKAGE);
            pair.beanName = replaceAll(JSP_NAME);
            break;
        case JS:
            pair.packageName = replaceAll(JS_PACKAGE);
            pair.beanName = replaceAll(JS_NAME);
            break;
        case POOBJ:
            pair.beanName = replaceAll(PO_OBJ_NAME);
        case DAOOBJ:
            pair.beanName = replaceAll(DAO_OBJ_NAME);
        case SERVICEOBJ:
            pair.beanName = replaceAll(SERVICE_OBJ_NAME);
        }
        return pair;
    }
    
    public String getUri(Path path) throws IOException {
        String uri = "";
        switch(path) {
        case PO:
            uri = "/po.ftl";
            break;
        case SQLMAP:
            uri = "sqlmap.ftl";
            break;
        case IDAO:
            uri = "/dao.ftl";
            break;
        case DAOIMPL:
            uri = "/daoImpl.ftl";
            break;
        case ISERVICE:
            uri = "/service.ftl";
            break;
        case SERVICEIMPL:
            uri = "/serviceImpl.ftl";
            break;
        case ACTION:
            uri = "/action.ftl";
            break;
        case JSP:
            uri = "/jsp.ftl";
            break;
        case JS:
            uri = "/js.ftl";
            break;
        default:
            break;
        }
        return uri;
    }
    
    private OutputStream getOutputStream(String prefix,String packageName,String fileName) throws IOException {
        String packageDir = packageName.replaceAll("\\.", File.separator);
        String path = prefix + File.separator + packageDir;
        File fullPath = new File(path);
        FileUtils.forceMkdir(fullPath);
        return FileUtils.openOutputStream(new File(fullPath + File.separator + fileName));
    }
    
    public OutputStream getOutputStream(Path path) throws IOException {
        String packageName = null;
        String fileName = null;
        String prefix = null;
        switch(path) {
        case PO:
            prefix = javaDir;
            packageName = this.getPOPackage();
            fileName = this.getPOName();
            break;
        case SQLMAP:
            prefix = javaDir;
            packageName = this.getSqlMapPackage();
            fileName = this.getSqlMapName();
            break;
        case IDAO:
            prefix = javaDir;
            packageName = this.getIDaoPackage();
            fileName = this.getIDaoName();
            break;
        case DAOIMPL:
            prefix = javaDir;
            packageName = this.getDaoImplPackage();
            fileName = this.getDaoImplName();
            break;
        case ISERVICE:
            prefix = javaDir;
            packageName = this.getIServicePackage();
            fileName = this.getIServiceName();
            break;
        case SERVICEIMPL:
            prefix = javaDir;
            packageName = this.getServiceImplPackage();
            fileName = this.getServiceImplName();
            break;
        case JSP:
            prefix = webDir;
            packageName = this.getJspDir();
            fileName = this.getJspName();
            break;
        case JS:
            prefix = webDir;
            packageName = this.getJsDir();
            fileName = this.getJsName();
            break;
        default:
            throw new UnknownError("未知的Enum类型");
        }
        return getOutputStream(prefix, packageName, fileName);
    }
    
    public final String getActionPackage() {
        return this.transferPair(Path.ACTION).packageName;
    }
    
    public final String getPOPackage() {
        return this.transferPair(Path.PO).packageName;
    }
    
    public final String getIDaoPackage() {
        return this.transferPair(Path.IDAO).packageName;
    }
    
    public final String getIServicePackage() {
        return this.transferPair(Path.ISERVICE).packageName;
    }
    
    public final String getDaoImplPackage() {
        return this.transferPair(Path.DAOIMPL).packageName;
    }
    
    public final String getServiceImplPackage() {
        return this.transferPair(Path.SERVICEIMPL).packageName;
    }
    
    public final String getJspDir() {
        return this.transferPair(Path.JSP).beanName;
    }
    
    public final String getJsDir() {
        return this.transferPair(Path.JS).beanName;
    }
    
    public final String getSqlMapPackage() {
        return this.transferPair(Path.SQLMAP).packageName;
    }
    
    public final String getPOName() {
        return this.transferPair(Path.PO).beanName;
    }
    
    public final String getActionName() {
        return this.transferPair(Path.ACTION).beanName;
    }
    
    public final String getIServiceName() {
        return this.transferPair(Path.ISERVICE).beanName;
    }
    
    public final String getIDaoName() {
        return this.transferPair(Path.IDAO).beanName;
    }
    
    public final String getServiceImplName() {
        return this.transferPair(Path.SERVICEIMPL).beanName;
    }
    
    public final String getDaoImplName() {
        return this.transferPair(Path.DAOIMPL).beanName;
    }
    
    public final String getJsName() {
        return this.transferPair(Path.JS).beanName;
    }
    
    public final String getJspName() {
        return this.transferPair(Path.JSP).beanName;
    }
    
    public String getSqlMapName() {
        return this.transferPair(Path.SQLMAP).beanName;
    }
    
    public String getPOObject() {
        return this.transferPair(Path.POOBJ).beanName;
    }
    
    public String getDaoObject() {
        return this.transferPair(Path.DAOOBJ).beanName;
    }
    
    public String getServiceObject() {
        return this.transferPair(Path.SERVICEOBJ).beanName;
    }
}
